/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Categoria.java
 *
 * Created on 16/11/2011, 01:14:43 PM
 */

package Vistas;

import Beans.Categoria;
import Controladores.Mcategoria;
import Imagenes.CtrlImagen;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Ryodans
 */
public class VistaCategoria extends javax.swing.JFrame {

    /** Creates new form Categoria */

    Mcategoria Mca = new Mcategoria();
    DefaultTableModel dlm = new DefaultTableModel();
    Categoria c = new Categoria();
    int indice=-1;
    int fila;

    public VistaCategoria() {
        initComponents();
    }
    public VistaCategoria(Mcategoria Mca) {
        initComponents();
        this.Mca=Mca;
        dlm.addColumn("Categorias");
        dlm.addColumn("Codigo");
        
        for(int x=0;x<Mca.getListCategoria().size();x++){
            Vector<String> vs = new Vector<String>();
            vs.add(Mca.getListCategoria().get(x).getNombre());
            vs.add(Mca.getListCategoria().get(x).getCodigo()+"");
            dlm.addRow(vs);
        }



    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new CtrlImagen("/Imagenes/a1.jpg");
        jPanelTransparente1 = new Imagenes.JPanelTransparente();
        jLabel1 = new javax.swing.JLabel();
        JTxtNombre = new javax.swing.JTextField();
        jLblCodigo = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanelTransparente3 = new Imagenes.JPanelTransparente();
        jBEliminarTodo = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanelTransparente5 = new Imagenes.JPanelTransparente();
        JBStop = new javax.swing.JButton();
        jBModificar = new javax.swing.JButton();
        jBEliminar = new javax.swing.JButton();
        jBSalir1 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Intefaz Control de Categorias de Empleado");

        jPanelTransparente1.setBorder(javax.swing.BorderFactory.createTitledBorder("Agregar Categoria"));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Nombre");

        javax.swing.GroupLayout jPanelTransparente1Layout = new javax.swing.GroupLayout(jPanelTransparente1);
        jPanelTransparente1.setLayout(jPanelTransparente1Layout);
        jPanelTransparente1Layout.setHorizontalGroup(
            jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addGroup(jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelTransparente1Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLblCodigo))
                    .addComponent(JTxtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanelTransparente1Layout.setVerticalGroup(
            jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLblCodigo)
                    .addComponent(jLabel2))
                .addGap(30, 30, 30)
                .addGroup(jPanelTransparente1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(JTxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        jBEliminarTodo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/stop.png"))); // NOI18N
        jBEliminarTodo.setText("ELIMINAR TODO");
        jBEliminarTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEliminarTodoActionPerformed(evt);
            }
        });

        jTable1.setModel(dlm);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanelTransparente3Layout = new javax.swing.GroupLayout(jPanelTransparente3);
        jPanelTransparente3.setLayout(jPanelTransparente3Layout);
        jPanelTransparente3Layout.setHorizontalGroup(
            jPanelTransparente3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTransparente3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(jBEliminarTodo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                .addGap(25, 25, 25))
        );
        jPanelTransparente3Layout.setVerticalGroup(
            jPanelTransparente3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelTransparente3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jBEliminarTodo, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        JBStop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/burn.png"))); // NOI18N
        JBStop.setText("CANCELAR");
        JBStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBStopActionPerformed(evt);
            }
        });

        ListSelectionModel lsm = jTable1.getSelectionModel();
        lsm.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                try{
                    if(e.getValueIsAdjusting())return;
                    ListSelectionModel ls=(ListSelectionModel )e.getSource();
                    if(ls.isSelectionEmpty()){
                        fila=-1;
                        jBModificar.setEnabled(false);
                    }else{
                        fila=ls.getMinSelectionIndex();
                        jBModificar.setEnabled(true);
                    }
                }catch(Exception z){
                }
            }
        });
        jBModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/edit.png"))); // NOI18N
        jBModificar.setText("MODIFICAR");
        jBModificar.setEnabled(false);
        jBModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBModificarActionPerformed(evt);
            }
        });

        ListSelectionModel lsms = jTable1.getSelectionModel();
        lsms.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                try{
                    if(e.getValueIsAdjusting())return;
                    ListSelectionModel lss=(ListSelectionModel )e.getSource();
                    if(lss.isSelectionEmpty()){
                        fila=-1;
                        jBEliminar.setEnabled(false);
                    }else{
                        fila=lss.getMinSelectionIndex();
                        jBEliminar.setEnabled(true);
                    }
                }catch(Exception z){
                }
            }
        });
        jBEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/stop.png"))); // NOI18N
        jBEliminar.setText("ELIMINAR");
        jBEliminar.setEnabled(false);
        jBEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEliminarActionPerformed(evt);
            }
        });

        jBSalir1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Desktop 2.png"))); // NOI18N
        jBSalir1.setText("SALIR");
        jBSalir1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalir1ActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Add.png"))); // NOI18N
        jButton1.setText("AGREGAR");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelTransparente5Layout = new javax.swing.GroupLayout(jPanelTransparente5);
        jPanelTransparente5.setLayout(jPanelTransparente5Layout);
        jPanelTransparente5Layout.setHorizontalGroup(
            jPanelTransparente5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelTransparente5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(jBModificar, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(jBEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(JBStop, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                    .addComponent(jBSalir1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanelTransparente5Layout.setVerticalGroup(
            jPanelTransparente5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTransparente5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(JBStop, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBSalir1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelTransparente1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jPanelTransparente5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(37, 37, 37)
                .addComponent(jPanelTransparente3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanelTransparente3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanelTransparente1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanelTransparente5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jMenu1.setText("Operaciones");
        jMenu1.add(jSeparator1);

        jMenuItem8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/exit.png"))); // NOI18N
        jMenuItem8.setText("Salir");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem8);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edicion");

        jMenuItem1.setText("Copiar");
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JBStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBStopActionPerformed
        //Cancelar
        this.Actualizar();
}//GEN-LAST:event_JBStopActionPerformed

    private void jBModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBModificarActionPerformed
        // Modificar Categoria
        if(jBModificar.getText().equals("MODIFICAR")){
            indice = jTable1.getSelectedRow();
             c = Mca.getListCategoria().get(indice);
            JTxtNombre.setText(c.getNombre());
            JTxtNombre.requestFocus();
            jBModificar.setText("GUARDAR");
            ImageIcon[] img_Btns = new ImageIcon[8];
            img_Btns[0] = new ImageIcon(getClass().getResource("/Imagenes/Floppy.png"));
            jBModificar.setIcon(img_Btns[0]);

        }else{
            Categoria ca=new Categoria(c.getCodigo(),JTxtNombre.getText());
            Mca.getListCategoria().set(indice, ca);
            //Mca.Modificar(indice, c.getCodigo(), c);
            indice=-1;
            c=new Categoria();
            
            Actualizar();
        }
    }//GEN-LAST:event_jBModificarActionPerformed

    private void jBEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEliminarActionPerformed
        // Eliminar
        Categoria cr=Mca.getListCategoria().get(jTable1.getSelectedRow());
        Mca.Eliminar(cr.getCodigo());
        Actualizar();
        
}//GEN-LAST:event_jBEliminarActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        // Salir
        this.dispose();
}//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jBEliminarTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEliminarTodoActionPerformed
        // Eliminar Todo
        int pregunta = JOptionPane.showConfirmDialog(null,"¿Seguro"
                + " que desea eliminar todas las categorias?","Eliminar",JOptionPane.YES_NO_OPTION);
        if(pregunta==0){
            this.cleanTabla();
            this.borrarLista();
        }


    }//GEN-LAST:event_jBEliminarTodoActionPerformed

    private void jBSalir1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSalir1ActionPerformed
        // // Salir
        this.dispose();
    }//GEN-LAST:event_jBSalir1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //Registrar
        if(JTxtNombre.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Porfavor, Rellene todo los Campos","Error",JOptionPane.ERROR_MESSAGE);
        }else{
            Categoria Ca = new Categoria();
            Ca.setNombre(JTxtNombre.getText());
            Ca.setCodigo(Mca.GenerarCodigo());
            Mca.Registrar(Ca);
            AgregarLineaTabla(Mca.getListCategoria());
            JTxtNombre.requestFocus();
        }
        
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaCategoria().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JBStop;
    private javax.swing.JTextField JTxtNombre;
    private javax.swing.JButton jBEliminar;
    private javax.swing.JButton jBEliminarTodo;
    private javax.swing.JButton jBModificar;
    private javax.swing.JButton jBSalir1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLblCodigo;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JPanel jPanel1;
    private Imagenes.JPanelTransparente jPanelTransparente1;
    private Imagenes.JPanelTransparente jPanelTransparente3;
    private Imagenes.JPanelTransparente jPanelTransparente5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private void Actualizar() {
        this.dispose();
        VistaCategoria Vc = new VistaCategoria(Mca);
        Vc.setLocationRelativeTo(null);
        Vc.setVisible(true);
    }

       private void AgregarLineaTabla(List<Categoria> Categorias) {
        this.cleanTabla();

        for(int x=0;x<Categorias.size();x++){
            Vector<String> v = new Vector<String>();
            v.add(Categorias.get(x).getNombre());
            v.add(Categorias.get(x).getCodigo()+"");
            dlm.addRow(v);
        }

    }

    public void cleanTabla(){

        dlm=(DefaultTableModel) jTable1.getModel();
        int x=dlm.getRowCount();
        for(int i=-1;i<(x-1);i++){
              dlm.removeRow(0);
        }
    
    }
    public void borrarLista(){
            Mca.getListCategoria().clear();
        try {
            Mca.grabar();
        } catch (IOException ex) {
            Logger.getLogger(VistaCategoria.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
